package kr.blogspot.stiner01.moduletutorial;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.TextView;

public class KeyTest extends Activity implements OnKeyListener
{
    private StringBuilder _builder = new StringBuilder();
    private TextView _textView = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        _textView = new TextView(this);
        _textView.setText("Press keys (if you have some)!");
        _textView.setOnKeyListener(this);
        _textView.setFocusableInTouchMode(true);
        _textView.requestFocus();
        
        setContentView(_textView);
    }
    
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        _builder.setLength(0);
        
        switch (event.getAction())
        {
        case KeyEvent.ACTION_DOWN:
            _builder.append("down, ");
            break;
            
        case KeyEvent.ACTION_UP:
            _builder.append("up, ");
            break;
        }
        
        _builder.append(event.getKeyCode());
        _builder.append(", ");
        _builder.append((char)event.getUnicodeChar());
        String text = _builder.toString();
        Log.d("KeyTest", text);
        _textView.setText(text);
        
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
            return false;
        else
            return true;
    }
}
